package com.ch.websocketdemo.socket.decode;

import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

/**
 * @author CH
 * @date 2021-10-21 15:12
 * @desc
 */
@Slf4j
public class ServerEncoder implements Encoder.Text<Object> {
    ObjectMapper objectMapper;

    @Override
    public void init(EndpointConfig endpointConfig) {
        this.objectMapper = SpringUtil.getBean(ObjectMapper.class);
    }

    @Override
    public void destroy() {

    }

    @SneakyThrows(Exception.class)
    @Override
    public String encode(Object object) {
        return objectMapper.writeValueAsString(object);
    }
}
